<?php

function smarty_function_pagination($params, &$smarty)
{
	$num	= 0;
	$page	= 1;
	$pages	= 1;
	$start	= 0;
	$limit	= 0;

	$div	= '|';

	foreach($params as $_key=>$_val) {
		switch($_key) {
			case 'pagination':
				$num	= $_val['num'];
				$page	= $_val['page'];
				$pages	= $_val['pages'];
				$start	= $_val['start'];
				$limit	= $_val['limit'];
				break;
		}
	}

	$html = null;

	if($pages > 1) {
		$html .= '<div class="pagination">';
		if($page != 1) {
			$html .= '<a href="'. page_url(1) .'">&laquo;</a> '. $div .' <a href="'. page_url($page - 1) .'">Prev</a> '. $div;
		}

		$end	= ($start + $limit) < $num ? ($start + $limit) : $num;

		$html .= ' Displaying '. ($start + 1) .' to '. $end .' - '. $num .' results found';

		if($page < $pages) {
			$html .= ' '. $div .' <a href="'. page_url($page + 1) .'">Next</a> '. $div .' <a href="'. page_url($pages) .'">&raquo;</a>';
		}
		$html .= '</div>';
	}

	return trim($html);
}

function page_url($page)
{
	static $url = null;
	if($url === null) {
		$url	= 'index.php';
		if(isset($_SERVER['QUERY_STRING'])) {
			$url .= '?';
			$qs		= $_SERVER['QUERY_STRING'];
			if(preg_match("/page=(\d+)/i", $qs)) {
				$qs = preg_replace("/page=(\d+)/i", 'page=%%', $qs);
				$url .= $qs;
			} else {
				$url .= $qs .'&amp;page=%%';
			}
		} else {
			$url .= '?page=%%';
		}
	}
	return str_replace('%%', $page, $url);
}

?>